// API配置文件
const API_BASE_URL = ''; // 使用相对路径，配合vite代理

export const API_CONFIG = {
  BASE_URL: API_BASE_URL,
  
  // 骑手相关接口
  RIDER: {
    REGISTER: `${API_BASE_URL}/api/rider/register`,
    LOGIN: `${API_BASE_URL}/api/rider/login`,
    PROFILE: (id) => `${API_BASE_URL}/api/rider/profile/${id}`,
    CHANGE_PASSWORD: `${API_BASE_URL}/api/rider/change-password`,
    LIST: `${API_BASE_URL}/api/rider/list`,
    ADD: `${API_BASE_URL}/api/rider/add`,
    UPDATE: (id) => `${API_BASE_URL}/api/rider/update/${id}`,
    DELETE: (id) => `${API_BASE_URL}/api/rider/delete/${id}`,
    // 骑手在线状态管理接口
    ONLINE: `${API_BASE_URL}/api/rider/online`,
    OFFLINE: `${API_BASE_URL}/api/rider/offline`,
    UPDATE_LOCATION: `${API_BASE_URL}/api/rider/update-location`,
    GET_STATUS: (id) => `${API_BASE_URL}/api/rider/status/${id}`,
    UPDATE_WORK_STATUS: `${API_BASE_URL}/api/rider/work-status`,
    // 抢单接口
    GRAB_ORDER: `${API_BASE_URL}/api/rider/grab-order`,
  },
  
  // 订单相关接口
  ORDER: {
    LIST: `${API_BASE_URL}/api/admin/orders`,
    ADD: `${API_BASE_URL}/api/admin/orders`,
    UPDATE: (id) => `${API_BASE_URL}/api/admin/orders/${id}`,
    UPDATE_STATUS: (id) => `${API_BASE_URL}/api/admin/orders/${id}/status`,
    UPDATE_EXCEPTION: (id) => `${API_BASE_URL}/api/admin/orders/${id}/exception`,
    DELETE: (id) => `${API_BASE_URL}/api/admin/orders/${id}`,
    GRAB: `${API_BASE_URL}/api/rider/grab-order`,
  }
};

// 通用请求函数
export const request = async (url, options = {}) => {
  const defaultOptions = {
    headers: {
      'Content-Type': 'application/json',
    },
  };

  const finalOptions = {
    ...defaultOptions,
    ...options,
    headers: {
      ...defaultOptions.headers,
      ...options.headers,
    },
  };

  try {
    const response = await fetch(url, finalOptions);
    const data = await response.json();
    
    if (!response.ok) {
      throw new Error(data.message || '请求失败');
    }
    
    return data;
  } catch (error) {
    console.error('API请求错误:', error);
    throw error;
  }
}; 